package priorityQueue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;

public class Test {
	    public int queue[];
	    
		private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
		
		private void grow(int minCapacity) {
			int oldCapacity = queue.length;
			// Double size if small; else grow by 50%
			int newCapacity = oldCapacity + ((oldCapacity < 64) ?
											(oldCapacity + 2) :
											(oldCapacity >> 1));
			// overflow-conscious code
			if (newCapacity - MAX_ARRAY_SIZE > 0)
				newCapacity = hugeCapacity(minCapacity);
			
			queue = Arrays.copyOf(queue, newCapacity);
		}
		
		private static int hugeCapacity(int minCapacity) {
			if (minCapacity < 0) // overflow
				throw new OutOfMemoryError();
			return (minCapacity > MAX_ARRAY_SIZE) ?Integer.MAX_VALUE :MAX_ARRAY_SIZE;
		}


	public static void main2(String[] args) {
		int[] arr = {4,1,9,2,8,0,7,3,6,5};

		PriorityQueue<Integer> q = new PriorityQueue<>(arr.length);
		for (int e: arr) {
			q.offer(e);
		} 
		
		System.out.println(q.size()); // 打印优先级队列中有效元素个数
		System.out.println(q.peek()); // 获取优先级最高的元素
		q.poll();
		q.poll();
		System.out.println(q.size()); // 打印优先级队列中有效元素个数
		System.out.println(q.peek()); // 获取优先级最高的元素
		q.offer(0);
		System.out.println(q.peek()); // 获取优先级最高的元素
		// 将优先级队列中的有效元素删除掉，检测其是否为空
		q.clear();
		if(q.isEmpty()){
			System.out.println("优先级队列已经为空!!!");
		} 
		else{
			System.out.println("优先级队列不为空");
		}
		
		
	}
	public static void main1(String[] args) {
		//创建空的优先级队列
		PriorityQueue<Integer> q1=new PriorityQueue<Integer>();
		//创建空的优先级队列，但容量默认为size
		PriorityQueue<Integer> q2=new PriorityQueue<Integer>(100);
		
		ArrayList<Integer> list=new ArrayList<>();
		list.add(1);
		list.add(2);
		list.add(3);
		PriorityQueue<Integer> q3=new PriorityQueue<Integer>(list);
	}
}
